SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[spABMIncumbencias]

@op INT, -- 1 = Alta; 2 = Baja
@grupo INT,
@incumbencia INT,
@cargo INT,
@asignatura INT

AS
SET NOCOUNT ON
DECLARE @error INT

BEGIN TRANSACTION

IF @op =1 BEGIN
	IF NOT EXISTS(SELECT * FROM tb_Incumbencias WHERE CargoGenerico = @cargo AND AsignaturaGenerica = @asignatura AND GrupoIncumbencia = @Grupo) BEGIN
		INSERT INTO tb_Incumbencias(GrupoIncumbencia, CargoGenerico, AsignaturaGenerica) VALUES (@grupo, @cargo, @asignatura)

		IF @@error <> 0 BEGIN
			SELECT @error = @@error
			GOTO Salir
		END
	END
END
IF @op = 2 BEGIN
	DELETE FROM tb_Incumbencias WHERE IdIncumbencia = @incumbencia

	IF @@error <> 0 BEGIN
		SELECT @error = @@error
		GOTO Salir
	END
END

Salir:
IF @error <> 0 
	ROLLBACK TRANSACTION
ELSE
	COMMIT TRANSACTION
GO
